package com.edu.start;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.ArrayList;
import java.util.List;

class Emetteur extends Thread{
	String host;
	InetAddress  groupeIP;
	int port;
	MulticastSocket socketEmission;
	String nom;

	Emetteur(InetAddress groupeIP, int port, String nom) throws Exception {
		this.groupeIP = groupeIP;
		this.port = port;
		this.nom = nom;
		socketEmission = new MulticastSocket();
		socketEmission.setTimeToLive(15); 
		start();
	}

	public void run() {
		String entreeClavier;
		try {
			host = InetAddress.getLocalHost().getHostName();
			entreeClavier = host; 
			String texte = host;
			emettre(texte);
		}
		catch (Exception exc) {
			System.out.println(exc);
		}
	} 

	void emettre(String texte) throws Exception {
		
		byte[] contenuMessage;
		DatagramPacket message;

		ByteArrayOutputStream sortie = new ByteArrayOutputStream(); 
		texte = nom + " : " + texte ;
		(new DataOutputStream(sortie)).writeUTF(texte); 
		contenuMessage = sortie.toByteArray();
		message = new DatagramPacket(contenuMessage, contenuMessage.length, groupeIP, port);
		socketEmission.send(message);
	}
}
